// pages/integralconfirm/integralconfirm.js
var app = getApp();
var GlobalData = require('../../utils/GlobalData.js');
var dataManager = require('../../utils/dataManager.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    postage: 0,
    showCourierType: false,
    courierType: [],
    buyinfo: [],
    hight_btn: "",
    shopcarIntegral: {},
    price: { "bonuspoints": 0, "cash": 0 },
    /**配送方式 */
    type: { "name": "请选择配送方式" },
    tipText: "",
    address: { "name": "", "phone": "", "address": "请填写收货地址", "isfirst": "isfirst", "areas": "" }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },
  /**获取收货地址成功的回调函数 */
  onAddressComplete() {
    if (GlobalData.shoppingAddress) {
      this.data.address = GlobalData.shoppingAddress;
    } else {
      if (GlobalData.addressArr.length) {
        for (let i = 0; i < GlobalData.addressArr.length; i++) {
          if (GlobalData.addressArr[i].isdefault) {
            this.data.address = GlobalData.clone(GlobalData.addressArr[i]);
            break;
          }
        }
        if (this.data.address.isfirst) {
          this.data.address = GlobalData.clone(GlobalData.addressArr[0]);
        }
      }
    }
    this.setData({
      address: this.data.address
    })

  },
  /**添加地址 记录*/
  goAppendAddress() {
    if (GlobalData.addressArr.length) {
      dataManager.UseNavigateTo("/pages/addressmanage/addressmanage?url=integralconfirm", false)
      // this.router.navigateByUrl("addressmanage/productconfirm" + GlobalData._random());
    } else {
      GlobalData.addressTitle = 0
      dataManager.UseNavigateTo("/pages/appendaddress/appendaddress", false)
      // this.router.navigateByUrl("appendaddress/productconfirm" + GlobalData._random());
    }

  },
  /**备注 */
  //获取密码
  bindPassWordInput: function (e) {
    var index = parseInt(e.currentTarget.dataset.index)
    var code = e.detail.value.trim();
    // for (let i = 0; i < this.data.buyinfo.length; i++) {
    this.data.buyinfo[index].remark = code;

    // }
    this.setData({
      buyinfo: this.data.buyinfo
    })
  },
  /**提交订单 */
  onSubmitOrder() {
    if (!this.data.address.id) {
      app.onshowModal("请添加收货地址");
      return;
    }
    let arr = [];
    for (let i = 0; i < this.data.buyinfo.length; i++) {
      let obj = {};
      obj["id"] = this.data.buyinfo[i].goodsid;
      obj["num"] = this.data.buyinfo[i].num;
      obj["bonuspoints"] = this.data.buyinfo[i].bonuspoints;
      obj["cash"] = this.data.buyinfo[i].cash;
      obj["remark"] = this.data.buyinfo[i].remark;
      arr.push(obj);
    }
    let productStr = JSON.stringify(arr);
    if (!this.data.tipText) {
      this.data.tipText = "     ";
    }
    let url = { method: "placeanorder", Token: app.getToken(), addressid: this.data.address.id, items: productStr, deliverytype: 1, remark: this.data.tipText, isdirectbuy: GlobalData.isdirectbuy };
    dataManager.OnRequestPost(url, this.onSubmitOrderComplete);

  },
  onSubmitOrderComplete(data) {
    GlobalData.integralOrder = GlobalData.clone(data)
    GlobalData.order = {};
    GlobalData.order["orderno"] = data.orderno;
    GlobalData.order["money"] = data.cash;
    GlobalData.order["bonuspoints"] = data.bonuspoints;
    GlobalData.order["time"] = data.createtime;
    GlobalData.PayReason = 5;
    dataManager.UseNavigateTo("/pages/payment/payment?url=integralconfirm",false)

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    /**获取默认的收货地址 */
    dataManager.rigisterObserver("getshippingaddress", this.onAddressComplete, this);
    dataManager.getAddressData()
    this.data.buyinfo = GlobalData.buyInfo;
    let id = this.data.buyinfo[0];
    this.data.postage = id.courierFees;
    for (let i = 0; i < this.data.buyinfo.length; i++) {
      this.data.buyinfo[i]["remark"] = "";
      for (let j = 0; j < this.data.buyinfo[i].imgpath.length; j++) {
        if (this.data.buyinfo[i].imgpath[j].sortno == 3) {
          this.data.buyinfo[i]['img'] = this.data.buyinfo[i].imgpath[j].path;
        }
      }
      if (id.goodsid != this.data.buyinfo[i].goodsid) {
        id = this.data.buyinfo[i];
        this.data.postage += this.data.buyinfo[i].courierFees;
      }
    }
    this.data.price = GlobalData.clone(GlobalData.price);
    this.setData({
      buyinfo: this.data.buyinfo,
      postage: this.data.postage,
      price: this.data.price
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})